
package com.hlkj.pay.service.payment.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.pay.dto.payment.*;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.payment.MerchantPaymentStatisticsMapper;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentStatisticsDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.payment.IMerchantPaymentStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/11/20 13:44
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantPaymentStatisticsServiceImpl extends CommonSnFilterService implements IMerchantPaymentStatisticsService {

    private final MerchantPaymentStatisticsMapper paymentStatisticsMapper;

    @Override
    public PageResult<MerchantPaymentPageDto> queryPage(MerchantPaymentStatisticsPageQueryDto statisticsPageQueryDto) {
        return null;
    }

    @Override
    public PageResult<MerchantPaymentStatisticsDto> queryDetailPage(MerchantPaymentStatisticsPageQueryDto statisticsPageQueryDto) {
        return null;
    }

    @Override
    public List<MerchantPaymentStatisticsDO> queryList(Integer type, String relationId, long time) {
        MerchantPaymentStatisticsPageQueryDto orderStatisticsQueryDto = new MerchantPaymentStatisticsPageQueryDto();
        orderStatisticsQueryDto.setType(type);
        orderStatisticsQueryDto.setTime(time);
        orderStatisticsQueryDto.setRelationId(relationId);
        return paymentStatisticsMapper.selectList(buildWrapper(orderStatisticsQueryDto));
    }

    @Override
    public List<MerchantPaymentStatisticsDO> queryList(String sn, Integer type, String relationId, Set<Long> timeKeys) {
        MerchantPaymentStatisticsPageQueryDto orderStatisticsQueryDto = new MerchantPaymentStatisticsPageQueryDto();
        orderStatisticsQueryDto.setSn(sn);
        orderStatisticsQueryDto.setType(type);
        orderStatisticsQueryDto.setRelationId(relationId);
        orderStatisticsQueryDto.setTimes(timeKeys);
        return paymentStatisticsMapper.selectList(buildWrapper(orderStatisticsQueryDto));
    }

    @Override
    public void addPaymentDetail(MerchantPaymentStatisticsDO merchantPaymentDetailDO) {
        paymentStatisticsMapper.insert(merchantPaymentDetailDO);
    }

    @Override
    public void addPaymentList(List<MerchantPaymentStatisticsDO> merchantPaymentDtos) {
        paymentStatisticsMapper.insertBatch(merchantPaymentDtos);
    }

    @Override
    public void deleteByTime(long time, String sn, String remark) {
        MerchantPaymentStatisticsPageQueryDto orderStatisticsQueryDto = new MerchantPaymentStatisticsPageQueryDto();
        orderStatisticsQueryDto.setSn(sn);
        orderStatisticsQueryDto.setTime(time);
        List<MerchantPaymentStatisticsDO> statisticsList = paymentStatisticsMapper.selectList(buildWrapper(orderStatisticsQueryDto));
        List<Long> ids = statisticsList.stream().map(statisticsDO -> statisticsDO.getId()).collect(Collectors.toList());
        paymentStatisticsMapper.updateRemark(ids, remark);
    }

    @Override
    public void updatePaymentDetail(MerchantPaymentStatisticsDO merchantPaymentDetailDO) {
        merchantPaymentDetailDO.setUpdateTime(System.currentTimeMillis());
        paymentStatisticsMapper.updateStatistics(merchantPaymentDetailDO);
    }

    @Override
    public MerchantPaymentStatisticsDO queryOne(String sn, Integer type, String relationId, Long time) {
        MerchantPaymentStatisticsPageQueryDto orderStatisticsQueryDto = new MerchantPaymentStatisticsPageQueryDto();
        orderStatisticsQueryDto.setSn(sn);
        orderStatisticsQueryDto.setTime(time);
        orderStatisticsQueryDto.setType(type);
        orderStatisticsQueryDto.setRelationId(relationId);
        return paymentStatisticsMapper.selectOne(buildWrapper(orderStatisticsQueryDto));
    }

    /**
     * 查询条件封装
     *
     * @param merchantPaymentPageQueryDto
     * @return
     */
    LambdaQueryWrapper<MerchantPaymentStatisticsDO> buildWrapper(MerchantPaymentStatisticsPageQueryDto merchantPaymentPageQueryDto) {
        filterSn(merchantPaymentPageQueryDto);
        LambdaQueryWrapperX<MerchantPaymentStatisticsDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantPaymentStatisticsDO::getDeleteFlag, CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eqIfPresent(MerchantPaymentStatisticsDO::getType, merchantPaymentPageQueryDto.getType());
        queryWrapper.eqIfPresent(MerchantPaymentStatisticsDO::getSn, merchantPaymentPageQueryDto.getSn());
        queryWrapper.inIfPresent(MerchantPaymentStatisticsDO::getSn, merchantPaymentPageQueryDto.getSnList());
        queryWrapper.eqIfPresent(MerchantPaymentStatisticsDO::getRelationId, merchantPaymentPageQueryDto.getRelationId());
        queryWrapper.inIfPresent(MerchantPaymentStatisticsDO::getRelationId, merchantPaymentPageQueryDto.getRelationIds());
        queryWrapper.eqIfPresent(MerchantPaymentStatisticsDO::getTime, merchantPaymentPageQueryDto.getTime());
        queryWrapper.inIfPresent(MerchantPaymentStatisticsDO::getTime, merchantPaymentPageQueryDto.getTimes());
        queryWrapper.geIfPresent(MerchantPaymentStatisticsDO::getTime, merchantPaymentPageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(MerchantPaymentStatisticsDO::getTime, merchantPaymentPageQueryDto.getEndQueryTime());
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param merchantPaymentPageQueryDto
     * @return
     */
    QueryWrapper<MerchantPaymentDetailDto> buildDetailWrapper(MerchantPaymentPageQueryDto merchantPaymentPageQueryDto) {
        filterSn(merchantPaymentPageQueryDto);
        QueryWrapperX<MerchantPaymentDetailDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.eq("b.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eq("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eqIfPresent("a.sn", merchantPaymentPageQueryDto.getSn());
        queryWrapper.eqIfPresent("a.entry_type", merchantPaymentPageQueryDto.getEntryType());
        queryWrapper.inIfPresent("a.sn", merchantPaymentPageQueryDto.getSnList());
        queryWrapper.eqIfPresent("a.payment_id", merchantPaymentPageQueryDto.getPaymentId());
        queryWrapper.eqIfPresent("a.provider_code", merchantPaymentPageQueryDto.getProviderCode());
        queryWrapper.eqIfPresent("a.channel_code", merchantPaymentPageQueryDto.getChannelCode());
        queryWrapper.eqIfPresent("a.app_id", merchantPaymentPageQueryDto.getAppId());
        queryWrapper.eqIfPresent("a.mch_channel_code", merchantPaymentPageQueryDto.getMchChannelCode());
        queryWrapper.eqIfPresent("b.status", merchantPaymentPageQueryDto.getStatus());
        queryWrapper.geIfPresent("b.pay_amount", merchantPaymentPageQueryDto.getMinPayAmount());
        queryWrapper.leIfPresent("b.pay_amount", merchantPaymentPageQueryDto.getMaxPayAmount());
        queryWrapper.geIfPresent("b.create_time", merchantPaymentPageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("b.create_time", merchantPaymentPageQueryDto.getEndQueryTime());
        queryWrapper.eqIfPresent("b.ins_order_no", merchantPaymentPageQueryDto.getInsOrderNo());
        queryWrapper.eqIfPresent("b.third_payment_id", merchantPaymentPageQueryDto.getThirdPaymentId());
        queryWrapper.eqIfPresent("b.detail_payment_id", merchantPaymentPageQueryDto.getDetailPaymentId());
        queryWrapper.eqIfPresent("b.mch_order_no", merchantPaymentPageQueryDto.getMchOrderNo());
        queryWrapper.eqIfPresent("b.channel_order_id", merchantPaymentPageQueryDto.getChannelOrderId());
        queryWrapper.eqIfPresent("a.payment_type", merchantPaymentPageQueryDto.getPaymentType());
        return queryWrapper;
    }

}
